<?php

/*
 * Contains General Functions
 * @version 1.0
 */

/*
 * Unregisters globals to ensure security of the site
 */

function unregister_globals() {
    if (!ini_get('register_globals'))
        return;

    if (isset($_REQUEST['GLOBALS']))
        die('GLOBALS overwrite attempt detected');

    // Variables that shouldn't be unset
    $no_unset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');

    $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array() );
    foreach ($input as $k => $v)
        if (!in_array($k, $no_unset) && isset($GLOBALS[$k])) {
            $GLOBALS[$k] = null;
            unset($GLOBALS[$k]);
        }
}

/*
 * Strips slasses of the given array
 */

function strip_slashes_extended($array) {
    if (is_array($array)) {
        foreach ($array as $k => &$v) {
            $v = strip_slashes_extended($v);
        }
    } else {
        $array = stripslashes($array);
    }
    return $array;
}

/*
 * OUTPUTS CONENTS AFTER GZIPPING THEM
 */

function gzip_output() {

    $HTTP_ACCEPT_ENCODING = $_SERVER['HTTP_ACCEPT_ENCODING'];
    if (headers_sent()) {
        $encoding = false;
    } elseif (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) {
        $encoding = 'x-gzip';
    } elseif (strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false) {
        $encoding = 'gzip';
    } else {
        $encoding = false;
    }

    if ($encoding) {
        $contents = ob_get_contents();
        ob_end_clean();
        header('Content-Encoding: ' . $encoding);
        print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
        $size = strlen($contents);
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, $size);
        echo $contents;
        exit();
    } else {
        ob_end_flush();
        exit();
    }
}

/*
 * Retrieves Site URL
 */

function site_url($protocol='http://') {
    //return SITE_URL;
    
    if(params::getParam('https')===true){
     $protocol='https://';
        return str_replace('http://','https://',SITE_URL);
    }
    return $protocol . $_SERVER['HTTP_HOST'].'/';
}

/*
 * Returns site Path.
 */

function site_path() {
    return APP_PATH;
}

/*
 * Returns Content URL
 */

function content_url() {
    return site_url() . 'content/';
}

/*
 * Returns path for the content folder
 */

function content_path() {
    if (!defined('CONTENT_PATH')) {
        return CONTENT_PATH;
    }
    return APP_PATH . 'includes' . DS;
}

/*
 * Returns Current Theme path
 */

function current_theme_path() {

    $path = THEME_PATH . 'default' . DS; //
    if (is_dir($path . 'index.php')) {
        echo 'The specified theme couldn\'t found at ' . $path;
        die();
    }
    return $path;
}

/*
 * Returns Current Theme URL
 */

function current_theme_url() {
    //. get current theme name and folder
    $theme = 'default';
    return theme_url() . $theme . '/';
}

/*
 * Returns Theme Path
 */

function theme_path() {
    return THEME_PATH;
}

/*
 * Returns Theme URL for Front End;
 */

function theme_url() {
    return content_url() . 'themes/';
}

/*
 * Returns Include Path
 */
function include_path() {
    return INCLUDE_PATH;
}
/*
 * Returns code path
 */
function code_path(){
    return include_path().'code'.DS;
}
/*
 * Retrieves class path
 */

function class_path() {
    return CLASS_PATH;
}

/*
 * Retrieves Current theme image Path
 */

function current_theme_image_path() {
    return current_theme_path() . 'images' . DS;
}

/*
 * Retrieves frontend url fro images based on current theme
 */

function current_theme_image_url() {
    return current_theme_url() . 'images/';
}

/*
 * Global function for image url which retrives current theme images folder
 */

function image_url() {
    return current_theme_image_url();
}

function image_path(){
    return current_theme_image_path();
}